<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
    <van-tab :title="channel.name" v-for="channel in channels" :key="channel.id">
      <!-- 文章列表 -->
      <article-list :channel="channel" />
    </van-tab>
    <div slot="nav-right" class="placeholder"></div>
    <div slot="nav-right" class="hamburger-btn" @click="isEditChannelShow = true">
      <i class="iconfont icon-gengduo"></i>
    </div>

    </van-tabs>
    <!-- /标签页 -->
    <!-- 频道编辑 -->
        <van-popup
          round
          class="edit-channel-popup"
          v-model="isEditChannelShow"
          position="bottom"
          :style="{ height: '90%' }"
          closeable
          close-icon-position="top-left"
        >
        <ChannelEdit :my-channels='channels' :active='active' @updata-active='onUpdataActive' />
        </van-popup>
<!-- /频道编辑 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [], // 文章列表
      isEditChannelShow: false // 控制频道编辑的显示与隐藏
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.getUserChannels()
  },
  methods: {
   async getUserChannels () {
    try {
      // const { data } = await getUserChannels()
      // this.channels = data.data.channels
      let channels = []
      if (this.user) {
        // 已登录
        const { data } = await getUserChannels()
        channels = data.data.channels
        console.log(this.channels)
      } else {
        // 未登录，判断是否有本地数据
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (localChannels) {
          channels = localChannels
        } else {
          // 没有，请求默认列表
          const { data } = await getUserChannels()
        this.channels = data.data.channels
        }
        }
        this.channels = channels
     } catch (error) {
      this.$toast('获取频道列表失败')
    }
  },
  onUpdataActive (index, isEditChannelShow = false) {
    this.active = index
    this.isEditChannelShow = isEditChannelShow
  }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
   .van-tabs__wrap {
     position: fixed;
     top: 92px;
     left: 0;
     right: 0;
     z-index: 1;
     height: 82px !important;
   }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }
  .placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}
.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.iconfont {
    font-size: 33px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
.edit-channel-popup {
  padding-top: 100px;
	box-sizing: border-box;
}
}
</style>
